package com.itgacl.magic4j.modules.comm.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itgacl.magic4j.common.base.SuperController;
import com.itgacl.magic4j.common.bean.PageData;
import com.itgacl.magic4j.common.bean.PageParam;
import com.itgacl.magic4j.libcommon.annotation.Auth;
import com.itgacl.magic4j.libcommon.annotation.Log;
import com.itgacl.magic4j.libcommon.bean.R;
import com.itgacl.magic4j.libcommon.constant.Constants;
import com.itgacl.magic4j.modules.comm.dto.CommLicenseDTO;
import com.itgacl.magic4j.modules.comm.entity.CommLicense;
import com.itgacl.magic4j.modules.comm.query.CommLicenseQuery;
import com.itgacl.magic4j.modules.comm.service.CommLicenseService;
import com.itgacl.magic4j.modules.comm.vo.CommLicenseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Classname CommLicenseController
 * @Description license信息表 Controller
 * @Author Created by xudp (alias:孤傲苍狼) 290603672@qq.com
 * @Date 2021-12-26
 * @Version 1.0
 */
@Api(tags = "CommLicense管理")
//@Auth(name = "CommLicense管理")//在类上标注了@Auth注解后这个类下的所有接口将被AuthInterceptor拦截进行访问权限控制
@RestController
@RequestMapping("/api/comm/license")
public class CommLicenseController extends SuperController{

    @Autowired
    private CommLicenseService commLicenseService;

    /**
     * 创建
     * @param commLicense
     * @return
     */
    @ApiOperation("新增")
    @Log(operation="创建",remark = "创建CommLicense",moduleName = "CommLicense管理") //在方法上标注@Log注解用于记录操作日志
    @PostMapping
    public R<Void> create(@RequestBody @Validated(Constants.Create.class) CommLicenseDTO commLicense){
        commLicenseService.create(commLicense);
        return R.ok();
    }

    /**
     * 更新
     * @param commLicense
     * @return
     */
    @ApiOperation("修改")
    @Log(operation="修改",remark = "修改CommLicense",moduleName = "CommLicense管理")
    @PutMapping
    public R<Void> update(@RequestBody @Validated(Constants.Update.class) CommLicenseDTO commLicense){
        commLicenseService.update(commLicense);
        return R.ok();
    }

    /**
     * 根据ID查找
     * @param id
     * @return
     */
    @ApiOperation("根据ID查找")
    @Auth(isAuth = false)//不进行权限控制
    @GetMapping("/{id}")
    public R<CommLicenseVo> get(@PathVariable("id") Long id){
        CommLicenseVo commLicenseVo = commLicenseService.getCommLicenseById(id);
        return R.ok(commLicenseVo);
    }

    /**
     * 根据条件查询
     * @return
     */
    @ApiOperation("根据条件查询")
    @Auth(isAuth = false)//不进行权限控制
    @GetMapping
    public R<List<CommLicenseVo>> get(CommLicenseQuery query) {
        //构建查询条件
        QueryWrapper<CommLicense> queryWrapper = buildQueryWrapper(query);
        List<CommLicenseVo> commLicenseListVo = commLicenseService.getList(queryWrapper);
        return R.ok(commLicenseListVo);
    }

    /**
    * 根据ID批量删除
    * @param ids
    * @return
    */
    @ApiOperation("根据ID批量删除")
    @Log(operation="删除",remark = "根据ID删除CommLicense",moduleName = "CommLicense管理")
    @DeleteMapping("/{ids}")
    public R<Void> delete(@PathVariable("ids") Long[] ids){
        if(ids.length==1){
            commLicenseService.deleteById(ids[0]);
        }else {
            List<Long> idList = Arrays.asList(ids);
            commLicenseService.deleteByIds(idList);
        }
        return R.ok();
    }

    /**
      * 分页查询
      * @return
      */
    @ApiOperation("分页查询")
    @Auth(isAuth = false)//不进行权限控制
    @GetMapping(value = "/list")
    public R<PageData<CommLicenseVo>> pageList(CommLicenseQuery query, PageParam pageParam){
        //构建查询条件
        QueryWrapper<CommLicense> queryWrapper = buildQueryWrapper(query);
        PageData<CommLicenseVo> pageData = commLicenseService.pageList(pageParam,queryWrapper);//mybatisPlus分页查询
        return R.ok(pageData);
    }

    /**
     * 构建查询条件QueryWrapper
     * @param query
     * @return
     */
    private QueryWrapper<CommLicense> buildQueryWrapper(CommLicenseQuery query) {
        QueryWrapper<CommLicense> queryWrapper = new QueryWrapper<>();
        //构建查询条件
        if(query.getId() != null){
            queryWrapper.eq(CommLicense.ID,query.getId());
        }
        if(StrUtil.isNotBlank(query.getLicenseIp())){
            queryWrapper.like(CommLicense.LICENSE_IP,query.getLicenseIp());
        }
        if(StrUtil.isNotBlank(query.getLicenseCode())){
            queryWrapper.like(CommLicense.LICENSE_CODE,query.getLicenseCode());
        }
        if(query.getMode() != null){
            queryWrapper.eq(CommLicense.MODE,query.getMode());
        }
        if(query.getExpiryTime() != null){
            queryWrapper.eq(CommLicense.EXPIRY_TIME,query.getExpiryTime());
        }
        if(StrUtil.isNotBlank(query.getLicensePublicKey())){
            queryWrapper.like(CommLicense.LICENSE_PUBLIC_KEY,query.getLicensePublicKey());
        }
        if(StrUtil.isNotBlank(query.getLicensePrivateKey())){
            queryWrapper.like(CommLicense.LICENSE_PRIVATE_KEY,query.getLicensePrivateKey());
        }
        if(StrUtil.isNotBlank(query.getSeed())){
            queryWrapper.like(CommLicense.SEED,query.getSeed());
        }
        if(StrUtil.isNotBlank(query.getLicenseContent())){
            queryWrapper.like(CommLicense.LICENSE_CONTENT,query.getLicenseContent());
        }
        if(StrUtil.isNotBlank(query.getLicenseFileUrl())){
            queryWrapper.like(CommLicense.LICENSE_FILE_URL,query.getLicenseFileUrl());
        }
        if(query.getStatus() != null){
            queryWrapper.eq(CommLicense.STATUS,query.getStatus());
        }
        if(query.getTenantId() != null){
            queryWrapper.eq(CommLicense.TENANT_ID,query.getTenantId());
        }
        return queryWrapper;
    }
}